import Vue from 'vue';
import Vuex from 'vuex';
Vue.use(Vuex);
//初始化数据
const state = {
  count: 0
};

//改变状态的方法
const mutations = {
    add(state){
        state.count++;
    },
    minus(state){
        state.count--;
    }
};

const getters = {
    count:function(state){
        return state.count;
    }
};

//actions 进行异步操作
const actions = {
    //context上下文
    addAction(context){
        context.commit("add",9);
        setTimeout(() => {
            context.commit("minus")
        }, 3000);

    },
    reduceAction({commit}){
        commit("minus");
    }
    
}

var vuexStore = new Vuex.Store({
  state,
  mutations,
  getters,
  actions
});
// 使用 export default 封装，让外部可以访问
export default vuexStore;
